package cn.kai.pet.service.impl;

import cn.kai.auth.domain.Logininfo;
import cn.kai.org.domain.Employee;
import cn.kai.org.mapper.EmployeeMapper;
import cn.kai.pet.domain.Pet;
import cn.kai.pet.domain.PetDetail;
import cn.kai.pet.domain.PetOnlineAuditLog;
import cn.kai.pet.mapper.PetDetailMapper;
import cn.kai.pet.mapper.PetMapper;
import cn.kai.pet.mapper.PetOnlineAuditLogMapper;
import cn.kai.pet.service.IPetService;
import cn.kai.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kai
 * @since 2022-06-25
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
     * 新增方法
     * @param pet
     */
    @Override
    @Transactional
    public void save(Pet pet, Logininfo logininfo) {
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        pet.setShopId(employee.getShop_id());
        petMapper.save(pet);
        PetDetail petDetail = pet.getDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    public void update(Pet pet, Logininfo logininfo) {

    }

    /**
     * 宠物上架
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        //1.审核宠物
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            //审核宠物名字
            StringBuilder sb =  new StringBuilder();
            /*if(!BaiduAuditUtils.TextCensor(pet.getName())){
                sb.append(pet.getName()+"名字不合法！！");
            }
            //审核图片
            if(!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+pet.getResources().split(",")[0])){
                sb.append(pet.getName()+"图片不合法！！");
            }*/

            //1.1 审核不通过？ 记录审核日志，跳过，下一个继续
            if(sb.toString().length() > 0){
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setNote(sb.toString());
                petOnlineAuditLogMapper.save(log);
                continue;
            }
            //1.2 修改上架状态，记录上架时间
            Map<String, Object> map = new HashMap<>();
            map.put("id",id);
            map.put("onSaleTime",new Date());
            petMapper.onSale(map);
        }
    }

    /**
     * 宠物下架
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offSaleTime",new Date());
        petMapper.offSale(map);
    }
}
